//
//  UIColor+JwSkin.m
//  JwPack
//
//  Created by 陈警卫 on 2023/1/12.
//

#import "UIColor+JwSkin.h"
#import "JwSkinExer.h"

@implementation UIColor (JwSkin)

/// MARK: 根据colorKey获取颜色
+ (UIColor *)jw_skinColorKey:(NSString *)key {
    SEL sel = NSSelectorFromString(key);
    id class = kJwSKEColorClass;
    
    if (![class respondsToSelector:sel]) {
        return [UIColor blackColor];
    }
    
    IMP imp = [class methodForSelector:sel];
    JwSkinModel *model = ((id(*)(id, SEL))imp)(class, sel);
    NSDictionary *colorParam = model.param;
    if (colorParam == nil) {
        return [UIColor blackColor];
    }
    UIColor *color = [self jw_skinColorParam:colorParam];
    return color;
}

/// MARK: 根据colorParam获取颜色
+ (UIColor *)jw_skinColorParam:(NSDictionary *)param {
    NSString *currentSkin = kJwSkinExer.jw_currentSkin;
    NSString *value = param[currentSkin];
    if (value == nil) {
        value = param[@"default"] ?: @"000000";
    }
    return [self jw_skinColorHexString:value];
}

/// MARK: 十六进制字符串颜色
+ (UIColor *)jw_skinColorHexString:(NSString *)hexString {
    return [UIColor jw_skinColorHexString:hexString alpha:1.0];
}

/// MARK: 十六进制字符串颜色
+ (UIColor *)jw_skinColorHexString:(NSString *)hexString alpha:(CGFloat)alpha {
    
    // 除去收尾空格
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *colorString = [hexString stringByTrimmingCharactersInSet:set];
    
    colorString = [[colorString stringByReplacingOccurrencesOfString:@"#" withString:@""] uppercaseString];
    
    CGFloat tred = 0.0;
    CGFloat tblue = 0.0;
    CGFloat tgreen = 0.0;
    CGFloat talpha = 0.0;
    
    NSUInteger length = [colorString length];
    if (length != 3 && length != 4 && length != 6 && length != 8) {
        NSLog(@"#UIColor+JwSkin# Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8");
        return nil;
    }
    
    unsigned colorValue;
    [[NSScanner scannerWithString:colorString] scanHexInt:&colorValue];
    
    switch ([colorString length]) {
        case 3: // #RGB
            tred   = ((colorValue & 0xF00) >> 8)       / 15.0;
            tgreen = ((colorValue & 0x0F0) >> 4)       / 15.0;
            tblue  = ((colorValue & 0x00F))            / 15.0;
            talpha = alpha;
            break;
        case 4: // #ARGB
            tred   = ((colorValue & 0xF000) >> 12)     / 15.0;
            tgreen = ((colorValue & 0x0F00) >> 8)      / 15.0;
            tblue  = ((colorValue & 0x00F0) >> 4)      / 15.0;
            talpha = ((colorValue & 0x000F))           / 15.0;
            break;
        case 6: // #RRGGBB
            tred   = ((colorValue & 0xFF0000) >> 16)   / 255.0;
            tgreen = ((colorValue & 0x00FF00) >> 8)    / 255.0;
            tblue  = ((colorValue & 0x0000FF))         / 255.0;
            talpha = alpha;
            break;
        case 8: // #RRGGBBAA
            tred   = ((colorValue & 0xFF000000) >> 24) / 255.0;
            tgreen = ((colorValue & 0x00FF0000) >> 16) / 255.0;
            tblue  = ((colorValue & 0x0000FF00) >> 8)  / 255.0;
            talpha = ((colorValue & 0x000000FF))       / 255.0;
            break;
        default:
            return nil;
            break;
    }
    return [UIColor colorWithRed:tred green:tgreen blue:tblue alpha:talpha];
}

@end
